package com.nlp.visualization.crawler.pipeline;

import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.core.seg.ISegmentService;
import com.nlp.visualization.pojo.NLP.seg.SegmentEntity;
import com.nlp.visualization.service.IDataDiscourseService;
import com.nlp.visualization.service.IDataSegmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.LinkedList;
import java.util.List;

/**
 * @author LXM
 * @Title: ${file_name}
 * @Description: 处理分词任务的类
 * @date 2018/3/13下午8:35
 */
@Service
public class SegmentTask implements Pipeline {

    @Autowired
    IDataSegmentService segmentService;

    SegmentEntity segmentEntity;

    Logger logger = LoggerFactory.getLogger(SegmentTask.class);


    public SegmentEntity getSegmentEntity() {
        return segmentEntity;
    }

    public void setSegmentEntity(SegmentEntity segmentEntity) {
        this.segmentEntity = segmentEntity;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        try {
            String title = resultItems.get("title").toString();
            String content = resultItems.get("content").toString();
            List<String> list = new LinkedList<>();
            list.add(content);
            SegmentEntity entity = segmentService.seg(list, CONSTANTS.SEGMENT_TYPE, CONSTANTS.FILTER_NO);
            setSegmentEntity(entity);
        } catch (Exception e) {
            logger.error("爬虫抽取提取字段异常");
            e.printStackTrace();
        }
    }
}
